<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Forgetpsw extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model("User_model","user");
        $this->load->library('Easemob');
    }
	public function index(){}


    public function sendSMS(){
        // file_put_contents("mylog.txt", file_get_contents('php://input')."注册\r\n",FILE_APPEND);
        $params['reginMobile'] = $this->queryData['reginMobile'];
        $validate = new Validate([
            'reginMobile' => 'require|string',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        //判断用户是否存在
        if(!$this->user->check_user($params['reginMobile'])){
            $this->errorCode = 50008;
            $this->createApi();exit();
        };

        //判断当天本用户登录次数 不能超过三次
       /* $maxSendCnt = $this->config->item('maxSendCnt');
        if($this->user->checkSendCnt($reginMobile, $maxSendCnt) == false){
            $this->errorCode = API_ERROR_SMS_OVER;
            $this->createApi();exit();
        }*/

        require_once(APPPATH.'third_party/yuntongxun/SendTemplateSMS.php');
        $mcode = strtolower(random_string('numeric',6));
        //验证码生成,并执行发送短信，60秒有效期
        $datas = array($mcode,10);
        $res = sendTemplateSMS($params['reginMobile'],$datas,133051);
        $data = array("mcode"=>$mcode,"expiretime"=>time()+600);
        $this->user->saveSmsStatus($params['reginMobile'],$data,$maxSendCnt);
        if(empty($res)){
            $this->errorCode = 60001;
        }
        $this->createApi();
    }



    /**
     *------------------------------------------------------------------------------------------------------------------
     * @title：验证  验证码
     *         1）首先验证验证码有效期
     *         2）判断验证码是否正确
     * @action：/forgetpsw/sendSMS
     * @method：GET
     * @param：reginMobile             类型：STRING，必须：YES ，描述：手机号码
     * @author:：KevinBai
     *------------------------------------------------------------------------------------------------------------------
     */
    public function verifymcode(){
        $params['username'] =  $this->queryData['reginMobile'];
        $params['mcode'] = $this->queryData['mcode'] ? $this->queryData['mcode'] : "";
        file_put_contents("mylog.txt", json_encode($params)."\r\n",FILE_APPEND);
        $validate = new Validate([
            'username' => 'require|string',
            'mcode' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $mcodeInfo = $this->user->get_mcodeInfo($params['username']);
        //判断验证码是否正确
        if($params['mcode'] != $mcodeInfo['mcode']){
            $this->errorCode = 60004;
            $this->createApi();exit();
        }
        //判断是否过期
        if($mcodeInfo['expiretime'] < time()) {
            $this->errorCode = 60002;
            $this->createApi();exit;
        }
        //用户身份识别标示
        $useInfo = $this->user->get_userinfo("",$params['username']);
        $token = $this->user->getTokenByid($useInfo['AccountID']);
        file_put_contents("mylog.txt", "token:".$token."\r\n",FILE_APPEND);
	   $this->createApi(array("token"=>$token['token']));
    }

    /**
     *------------------------------------------------------------------------------------------------------------------
     * @title：执行手机修改密码功能
     *         1）首先修改密码
     *         2）环信删除用户，并重新添加用户
     *         3）返回token值
     * @action：/forgetpsw/do_modify
     * @method：POST
     * @param：Password             类型：STRING，必须：YES ，描述：密码
     * @param：token                类型：STRING，必须：YES ，描述：token值
     * @author:：KevinBai
     *------------------------------------------------------------------------------------------------------------------
     */
    public function do_modify(){
        $params['password'] = $this->queryData['password'];
        $params['token'] = $this->queryData['token'];
        file_put_contents("mylog.txt", json_encode($params)."\r\n",FILE_APPEND);
        $validate = new Validate([
            'password' => 'require|string',
            'token' => 'require|string',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $userInfo = $this->user->getUserInfoByToken($params['token']);
        if(empty($userInfo)){
            $this->errorCode = 60009;
            $this->createApi();exit();
        }
        $AccountID = $userInfo['AccountID'];
        $data = array(
            'password'=>md6($params['password'])
        );
        $res = $this->user->edit_user($AccountID,$data);
        if(empty($res)){
            $this->errorCode = 6010;
        }
        $this->createApi();
    }

}

/* End of file region.php */
/* Location: ./application/controllers/region.php */
